package springbook.learningtest.template;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Calculator {
	public String concatenateString(String filepath) throws IOException {
		LineCallback<String> concatCallback = new LineCallback<String>() {

			@Override
			public String doSomethingWhitLine(String line, String value) {
				
				return value + line;
			}
			
		};
		
		return lineReadTemplate(filepath, concatCallback, "");
	}
	
	public Integer calcSum(String filepath) throws IOException {
//		BufferedReaderCallback sumCallback = new BufferedReaderCallback() {
//			
//			@Override
//			public Integer doSomethingWithReader(BufferedReader reader)
//					throws IOException {
//				String lineNumber = null;
//				Integer sum = 0;
//				
//				while((lineNumber = reader.readLine()) != null) {
//					sum += Integer.parseInt(lineNumber);
//				}
//				return sum;
//			}
//		};
//		
//		
//		return fileReadTemplate(filepath, sumCallback);
		LineCallback<Integer> sumCallback = new LineCallback<Integer>() {

			@Override
			public Integer doSomethingWhitLine(String line, Integer value) {
				return value + Integer.valueOf(line);
			}
		};
		
		return lineReadTemplate(filepath, sumCallback, 0);
	}

	public Integer calcMultiply(String filepath) throws NumberFormatException, IOException {
//		BufferedReaderCallback multiplyCallback = new BufferedReaderCallback() {
//			
//			@Override
//			public Integer doSomethingWithReader(BufferedReader reader)
//					throws IOException {
//				String lineNumber = null;
//				Integer multiply = 1;
//				
//				while((lineNumber = reader.readLine()) != null) {
//					multiply *= Integer.parseInt(lineNumber);
//				}
//				return multiply;
//			}
//		};
//		
//		
//		return fileReadTemplate(filepath, multiplyCallback);
		
		LineCallback<Integer> multiplyCallback = new LineCallback<Integer>() {

			@Override
			public Integer doSomethingWhitLine(String line, Integer value) {
				return value * Integer.valueOf(line);
			}
		};
		
		return lineReadTemplate(filepath, multiplyCallback, 1);
	}

	public <T> T lineReadTemplate(String filepath, LineCallback<T> callback, T initVal) throws IOException {
		BufferedReader reader = null;
		
		try {
			
			reader = new BufferedReader(new FileReader(filepath));
			T rtn = initVal;
			
			String line = null;
			
			while((line = reader.readLine()) != null) {
				rtn = callback.doSomethingWhitLine(line, rtn);
			}
			
			return rtn;
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		} finally {
			if(reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}
	
	public Integer fileReadTemplate(String filepath, BufferedReaderCallback callback) throws IOException {
		BufferedReader reader = null;
		
		try {
			
			reader = new BufferedReader(new FileReader(filepath));
			Integer sum = callback.doSomethingWithReader(reader);
			return sum;
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		} finally {
			if(reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
